package cn.coopen.italktv.upgradeslencedemo;

import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import static android.content.ContentValues.TAG;

/**
 * 作者： qing
 * 创建日期：2017/12/15
 * 功能描述：
 */

public class SilentInstall {
	/**
	 * 执行具体的静默安装逻辑，需要手机ROOT。
	 * @param apkPath
	 *          要安装的apk文件的路径
	 * @return 安装成功返回true，安装失败返回false。
	 */
	public boolean install(String apkPath) {
		boolean result = false;
		DataOutputStream dataOutputStream = null;
		BufferedReader errorStream = null;
		try {
			// 申请su权限
			Process process = Runtime.getRuntime().exec("su");
			dataOutputStream = new DataOutputStream(process.getOutputStream());
			// 执行pm install命令
			String command = "pm install -r " + apkPath + "\n";
			dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
			dataOutputStream.flush();
			dataOutputStream.writeBytes("exit\n");
			dataOutputStream.flush();
			process.waitFor();
			errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			String msg = "";
			String line;
			// 读取命令的执行结果
			while ((line = errorStream.readLine()) != null) {
				msg += line;
			}
			Log.d("TAG", "install msg is " + msg);
			// 如果执行结果中包含Failure字样就认为是安装失败，否则就认为安装成功
			if (!msg.contains("Failure")) {
				result = true;
			}
		} catch (Exception e) {
			Log.e("TAG", e.getMessage(), e);
		} finally {
			try {
				if (dataOutputStream != null) {
					dataOutputStream.close();
				}
				if (errorStream != null) {
					errorStream.close();
				}
			} catch (IOException e) {
				Log.e("TAG", e.getMessage(), e);
			}
		}
		return result;
	}

	public String installApkSilent(File apk) {
		String[] installcmds = {"pm", "install", "-r", apk.getAbsolutePath()};
		String result = "";
		ProcessBuilder processBuilder = new ProcessBuilder(installcmds);
		Process process = null;
		InputStream errIs = null;
		InputStream inIs = null;

		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = -1;
			process = processBuilder.start();
			errIs = process.getErrorStream();
			while ((len = errIs.read()) != -1) {
				baos.write(len);
			}
			baos.write('\n');
			inIs = process.getInputStream();
			while ((len = inIs.read()) != -1) {
				baos.write(len);
			}
			byte[] data = baos.toByteArray();
			result = new String(data);
		} catch (IOException e) {
			e.printStackTrace();
			Log.e(TAG, "Apk file install failed!---" + apk.getName());

			return null;
		} finally {
			try {
				if (errIs != null) {
					errIs.close();
				}
				if (inIs != null) {
					inIs.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (process != null) {
				process.destroy();
			}
		}

		return result;
	}
}
